package com.wuzao.advice;

import lombok.extern.slf4j.Slf4j;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;

@ControllerAdvice
@Slf4j
public class MyControllerAdvice {
    @ExceptionHandler(value = Exception.class)
    public String exception() {
        log.info("@ControllerAdvice增强处理 @ExceptionHandler 监控所有Controller，任意Controller产生异常，都会进到这里");
        return "arithmetic";
    }

    @ModelAttribute(value = "message")
    public String modelAttribute() {
        log.info("@ControllerAdvice增强处理");
        return "@ModelAttribute 全局消息，任意Controller都会获取到";
    }

    @InitBinder
    public void timeFormat(WebDataBinder binder) {
        log.info("@ControllerAdvice增强处理 @InitBinder 所有的Controller的时间格式化都会进到这里处理");
        binder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
    }
}
